package com.tiantian.quanminxiu.dialog;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.EditInfoView;

@SuppressLint("NewApi")
public class Dialog_EditName {

	/**
	 * 重置密码弹出框
	 *
	 * @author Administrator
	 *
	 */
	private static PopupWindow popupWindow;// 根弹出框
	private static EditText et_changenickname_text;// 账号输入
	private static TextView tv_changenickname_cancle;// 验证码输入
	private static TextView tv_changenickname_sure;// 验证码输入
	private static View contentView;
	private static RelativeLayout pop_forgetpwd_root;
	static Context context;
	static int time = 60;

	public static EditInfoView mEditInfoView;
	/**
	 * 构造
	 *
	 * @param context
	 */
	private Dialog_EditName(Context context) {
		initPop(context, mEditInfoView);
	}

	/**
	 * 初始化组件
	 *
	 * @param context
	 */
	@SuppressLint("CutPasteId")
	private static void initPop(Context context, EditInfoView editInfoView) {
		mEditInfoView = editInfoView;
		contentView = LayoutInflater.from(context).inflate(
				R.layout.dialog_editname, null);
		et_changenickname_text = (EditText) contentView
				.findViewById(R.id.et_changenickname_text);
		et_changenickname_text.setText(UserData.Base.NickName);
		tv_changenickname_cancle = (TextView) contentView
				.findViewById(R.id.tv_changenickname_cancle);
		tv_changenickname_sure = (TextView) contentView
				.findViewById(R.id.tv_changenickname_sure);
		// 设置按钮的点击事件
		pop_forgetpwd_root = (RelativeLayout) contentView
				.findViewById(R.id.pop_forgetpwd_root);
		popupWindow = new PopupWindow();
		// 设置SelectPicPopupWindow弹出窗体的宽
		popupWindow.setWidth(LayoutParams.FILL_PARENT);
		// 设置SelectPicPopupWindow弹出窗体的高
		popupWindow.setHeight(LayoutParams.FILL_PARENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		popupWindow.setFocusable(true);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		popupWindow.setContentView(contentView);
		popupWindow.setOutsideTouchable(true);
		pop_forgetpwd_root.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				int height = contentView.findViewById(R.id.pop_forgetpwd)
						.getTop();
				int y = (int) event.getY();
				if (y < height) {
					hide();
					distroy();
				}
				return true;
			}
		});
		initAction();
	}

	public static void initAction() {
		tv_changenickname_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				hide();
				distroy();
			}
		});
		tv_changenickname_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (TextUtils.isEmpty(et_changenickname_text.getText().toString())) {
					Toast.makeText(context, "昵称不能为空", Toast.LENGTH_SHORT)
							.show();
				}else if(et_changenickname_text.getText().length()>8){
					Toast.makeText(context, "昵称长度不能超过8位", Toast.LENGTH_SHORT)
							.show();
				}else {
					mEditInfoView.setUserName(et_changenickname_text.getText().toString());
					hide();
					distroy();
				}
			}
		});
	}

	/**
	 * 显示pop
	 *
	 * @param view
	 * @param _context
	 */
	public static void show(View view, Context _context, EditInfoView editInfoView) {
		mEditInfoView = editInfoView;
		context = _context;
		if (null != popupWindow) {
			popupWindow.showAtLocation(view, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
		} else {
			initPop(context, editInfoView);
			popupWindow.showAtLocation(view, Gravity.BOTTOM
					| Gravity.CENTER_HORIZONTAL, 0, 0);
		}

	}

	/**
	 * 隐藏弹出框
	 */
	public static void hide() {
		if (null != popupWindow) {
			popupWindow.dismiss();
		}
	}

	/**
	 * 销毁弹出框
	 */
	public static void distroy() {
		popupWindow = null;
	}

}
